package cn.net.cfss.fgbp.provider.console.spi;

import cn.net.cfss.fgbp.api.console.constant.DeleteFlagEnum;
import cn.net.cfss.fgbp.api.console.dto.SysApplicationDto;
import cn.net.cfss.fgbp.api.console.inte.ISysApplicationService;
import cn.net.cfss.fgbp.api.console.vo.SysApplicationVo;
import cn.net.cfss.fgbp.provider.console.biz.SysApplicationDtoBiz;
import cn.net.cfss.fgbp.provider.console.utils.FgbpIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author oygq
 * @title: IApplicationServiceImpl
 * @description: TODO
 * @date 2019/4/8 10:12
 */
@RestController
@Slf4j
public class SysApplicationSpi implements ISysApplicationService {

    @Autowired
    SysApplicationDtoBiz sysApplicationDtoBiz;

    @Override
    public boolean insert(SysApplicationVo vo) {
        try {
            vo.setId(FgbpIdUtil.generateId());
            vo.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
            vo.setCreateTime(new Date());
            if (null == vo.getSort()){
                vo.setSort(0);
            }
            sysApplicationDtoBiz.insert(vo);
        } catch (Exception e) {
            log.error("应用新增错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", vo, true);
        return true;
    }

    @Override
    public boolean update(SysApplicationVo vo) {
        try {
            vo.setId(Long.valueOf(vo.getAppId()));
            vo.setUpdateTime(new Date());
            sysApplicationDtoBiz.updateSelectiveById(vo);
        } catch (Exception e) {
            log.error("应用修改错误:{}", e.getMessage());
            //有异常情况调用这个方保存日志
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", vo, true);
        return true;
    }

    @Override
    public boolean delete(Long appId) {
        SysApplicationVo vo = new SysApplicationVo();
        try {
            vo.setId(appId);
            vo.setDeleteFlag(DeleteFlagEnum.DEL);
            sysApplicationDtoBiz.updateSelectiveById(vo);
        } catch (Exception e) {
            log.error("应用删除错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", appId, true);
        return true;
    }

    @Override
    public SysApplicationDto queryById(Long appId) {
        /*SysApplicationVo vo = new SysApplicationVo();
        vo.setId(appId);
        vo.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        SysApplicationDto dto = sysApplicationDtoBiz.selectOne(vo);*/
        SysApplicationDto dto = sysApplicationDtoBiz.selectById(appId);
        log.info("请求参数:{}，返回参数：{}", appId, dto);
        return dto;
    }

    @Override
    public List<SysApplicationDto> queryList(SysApplicationVo vo) {
        if (null == vo){
            return new ArrayList<SysApplicationDto>();
        }
        vo.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        List<SysApplicationDto> dtoList = sysApplicationDtoBiz.queryList(vo);
        log.info("请求参数:{}，返回参数：{}", vo, dtoList);
        return dtoList;
    }

    @Override
    public List<Long> queryByRoleId(Long roleId) {
        List<SysApplicationDto> applicationDtos = sysApplicationDtoBiz.queryByRoleId(roleId);
        List<Long> appIds = new ArrayList<>();
        for (SysApplicationDto dto : applicationDtos) {
            appIds.add(dto.getId());
        }
        log.info("请求参数:{}，返回参数：{}", roleId, appIds);
        return appIds;
    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        try {
            sysApplicationDtoBiz.deleteBatch(ids);
        } catch (Exception e) {
            log.error("应用批量删除错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", ids, true);
        return true;
    }

    @Override
    public List<SysApplicationDto> queryByRoleId(String roleCode) {
        return null;
    }

    /**
     * 应用唯一性校验
     * @param permission
     * @return
     */
    @Override
    public boolean uniquenessCheck(String permission,Long id) {
        try {
            boolean flag = sysApplicationDtoBiz.check(permission,id);
            log.info("请求参数:permission= {}，id= {},返回参数：{}", permission,id, flag);
            return flag;
        } catch (Exception e) {
            log.error("应用唯一校验错误:{}", e.getMessage());
            return false;
        }

    }
}
